tongfusoft.updateFieldsetHasMetadata = function() {
}
tongfusoft.updateFieldsetHasMetadata.prototype = {

	metadataListTable : function() {
		common.post('fieldSetHasMetadata/query', {
			"profileId" : $("#profileId").val(),
			"fieldsetId" : $("#fieldsetId").val()
		}, function(data) {
			$("#fieldSetData").html('');
			var fieldSetData='';
			if (data.state == 'success') {
				$.each(data.data, function(index, item) {
					var fieldSetHtml = ' <tr>';
					fieldSetHtml = fieldSetHtml + '<td>'+item.metadataName +'</td>';
					fieldSetHtml = fieldSetHtml + '<td>'+item.dictkeyName +'</td>';
					if(item.isRequire === '1'){
						fieldSetHtml = fieldSetHtml + '<td>';
						fieldSetHtml = fieldSetHtml +    '<div class="checkbox c-checkbox">';
						fieldSetHtml = fieldSetHtml +      '<label> <input type="checkbox" checked="checked" name="isRequire" value="1"/> ';
						fieldSetHtml = fieldSetHtml +      '<span class="fa fa-check"></span>';
						fieldSetHtml = fieldSetHtml +      '</label>';
						fieldSetHtml = fieldSetHtml +    '</div>';
						fieldSetHtml = fieldSetHtml + '</td>';	
					}else {
						fieldSetHtml = fieldSetHtml + '<td>';
						fieldSetHtml = fieldSetHtml +    '<div class="checkbox c-checkbox">';
						fieldSetHtml = fieldSetHtml +      '<label> <input type="checkbox" name="isRequire" value="0"/> ';
						fieldSetHtml = fieldSetHtml +      '<span class="fa fa-check"></span>';
						fieldSetHtml = fieldSetHtml +      '</label>';
						fieldSetHtml = fieldSetHtml +    '</div>';
						fieldSetHtml = fieldSetHtml + '</td>';
					}
					fieldSetHtml = fieldSetHtml + '<td> <input class="form-control" type="text" id="metaSort" name="metaSort" value="'+ item.fmetaSort +'"/> </td>';
					fieldSetHtml = fieldSetHtml + '<td>'
					fieldSetHtml = fieldSetHtml + '<button metadataId="'+item.metadataId +'"'
					fieldSetHtml = fieldSetHtml + '" onclick="new tongfusoft.updateFieldsetHasMetadata().del(this)" type="button" data-toggle="modal" class="detBtn btn btn-default btn-sm "><em class="fa fa-trash"></em></button>'
					fieldSetHtml = fieldSetHtml + '<input type="hidden" name="metadataId" value="'+ item.metadataId  +'" /> '
					fieldSetHtml = fieldSetHtml + '</td>'
					fieldSetHtml = fieldSetHtml + '</tr>';
						
					fieldSetData =fieldSetData+fieldSetHtml;
				});
			}
			$("#metadataListTable").html(fieldSetData);
		});
	},
	
	// 提交数据
	submitFieldSetMetadata : function() {

		$.validator.setDefaults({
			submitHandler : function() {
				var initData = common.getData("fieldSetMetadataForm");
				initData['fieldsetId'] = $("#fieldsetId").val();
				var url = 'fieldSetHasMetadata/update';

				common.ajaxSubmitOnSuccess(initData, url, function(data) {
							
					if (data.state == 'success') {
						 toastr["success"]('保存成功!');
						
						window.location.href = common.getContextPath('manager/inputProfile/updateInputProfile/' + $("#profileId").val());
					} else if (data.code == 'repeat_error') {

						toastr["error"]('参数编码不能重复!');
					} else{
						toastr["error"]('保存失败!');
					}

				}, $("#oper").val())
			}
		});
		$("#fieldSetMetadataForm").validate();

		$("#submitFieldSetMetada").click(function() {
			    var bb = "";
				var temp = "";
				var isRequireTemp = "";
				var isRequireVal = "";
				var metaSortTemp="";
				var metaSortVal ="";
				var metadataId = document.getElementsByName("metadataId");
				var isRequire = document.getElementsByName("isRequire");
				var metaSort = document.getElementsByName("metaSort");
				for ( var i = 0; i < metadataId.length; i++) {
					   temp = metadataId[i].value;
					   
					   bb = bb + "," +temp;
					   
					   for (var j = 0; j < isRequire.length; j++) {
							if(isRequire[i].checked){
								isRequireTemp = "1";
							}else{
								isRequireTemp = "0";
							}
						  }
						   isRequireVal = isRequireVal + "," + isRequireTemp;
						   metaSortTemp = metaSort[i].value;
						   metaSortVal = metaSortVal + "," + metaSortTemp;
					
				 }
				$("#metadataIdList").val(bb);
				$("#isRequireList").val(isRequireVal);
				$("#metaSortList").val(metaSortVal);
			$("#fieldSetMetadataForm").submit();

		})
	},
	// 返回fieldSet页面
	returnFieldSet : function() {
		$("#returnBtn").click(function() {
			
			window.location.href = common.getContextPath('manager/inputProfile/updateInputProfile/' + $("#profileId").val());
		})

	},
	
	// 跳转添加元数据页面
	addMetadataBtn : function() {
		$("#addMetadataBtn").click(function() {
			
			window.location.href = common.getContextPath('manager/inputProfile/fieldsetMetadata/' + $("#fieldsetId").val() + "/edit");
		})
	},
	// 删除
	del : function(obj) {

		swal({
			title : "是否确认删除?",
			text : "",
			type : "warning",
			showCancelButton : true,
			confirmButtonColor : "#DD6B55",
			confirmButtonText : "删除",
			cancelButtonText : "取消",
			closeOnConfirm : true
		}, function() {
			common.ajaxSubmitOnSuccess({
				"metadataId" : $(obj).attr("metadataId"),
				"fieldsetId" :$("#fieldsetId").val()
			}, 'fieldSetHasMetadata/del', function(data) {

				if (data.state == 'success') {
					new tongfusoft.updateFieldsetHasMetadata().metadataListTable();
					
				}else{
					toastr["error"]('删除失败!');
				}

			}, "del")
		});

	},
	
}

$(function() {
	var obj = new tongfusoft.updateFieldsetHasMetadata();
	obj.metadataListTable();
	obj.returnFieldSet();
	obj.addMetadataBtn();
	obj.submitFieldSetMetadata();
})